/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

import java.util.Arrays;
import java.util.Collection;

import com.eelpo.common.util.BlankUtils;

/**
 * <pre>
 * 
 * JOQL语句表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class JpqlExpression implements ParameterCriterion {

	private String prefix;
	private String queryString;
	private Object[] values;

	private JpqlExpression(String prefix, String queryString) {
		this.prefix = prefix;
		this.queryString = queryString;
	}

	private JpqlExpression(String prefix, String queryString, Object[] values) {
		this.prefix = prefix;
		this.values = values;
		this.queryString = queryString;
	}

	public static JpqlExpression getAndJpqlExpression(String queryString) {
		return new JpqlExpression(AND, queryString);
	}

	public static JpqlExpression getAndJpqlExpression(String queryString, Object... values) {
		return new JpqlExpression(AND, queryString, values);
	}

	public static JpqlExpression getOrJpqlExpression(String queryString) {
		return new JpqlExpression(OR, queryString);
	}

	public static JpqlExpression getOrJpqlExpression(String queryString, Object... values) {
		return new JpqlExpression(OR, queryString, values);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getJpql(java.lang.
	 *      StringBuilder)
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String alias) {
		jpql.append(prefix);
		jpql.append(queryString);
		return jpql;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.ParameterCriterion#getParameters(java.util.
	 *      Collection)
	 */
	@Override
	public Collection<Object> getParameters(Collection<Object> parameters) {
		if (!BlankUtils.isBlank(values)) {
			parameters.addAll(Arrays.asList(values));
		}
		return parameters;
	}

}
